package com.example.checkiphone13order.ssx;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class GetHtmlDocument {
    public String  getHtml(String url) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();

            HttpGet httpGet = new HttpGet(url);
            //此网址设置了反爬虫，需要配置cookie才行
//            httpGet.setHeader("cookie","dssid2=33eb12e5-9c1b-4cda-8adb-9a327de01f4a; dssf=1; as_sfa=Mnxjbnxjbnx8emhfQ058Y29uc3VtZXJ8aW50ZXJuZXR8MHwwfDE; as_uct=0; s_vi=[CS]v1|30B29AE23ED715D4-400015B9D5066B67[CE]; as_pcts=RIef_1i9wB6XPvfTlbA+McynoE5mlKLvJgjkx4vPzw4aI2TB2-DWl6ZZ2YERGHA_jZYRKQwwtoQvqFo1U0n_cRu8B3Rcn1sNXbLsTv0evHJl4rLgDSH1mxZjr8G; geo=CN; as_dc=ucp1");
//            httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36");
//            httpGet.setHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");

            CloseableHttpResponse execute = httpClient.execute(httpGet);
            HttpEntity entity = execute.getEntity();



            String s = EntityUtils.toString(entity, StandardCharsets.UTF_8);
//            System.out.println(s);

            execute.close();
            httpClient.close();
            return s;
        }catch (Exception e){
            return null;
        }
    }
}
